<?php
/*
 * 
 *  管理文章分类
 * 
 */
class Category extends CI_Controller
{

	 
	function __construct()
	 {
	    parent::__construct();
	
	 }
	 
	 public function index()
	 {
	 	$this->load->helper(array('form','url'));
	 	$this->load->database();
	    $this->db->select('*');
	    $query = $this->db->get('category')->result_array();
	    $data['cate'] = $query;
	    //$html = $this->load->view('manage/index','',TRUE);
	    $html=$this->load->view('category/list',$data,TRUE);
	    //$html.=$this->load->view('manage/footer','',TRUE);
	    echo $html;
	 }
	 
	 public function post()
	 {
	 	$this->load->database();
	 	$this->load->helper(array('form','url'));
	 	$this->load->library('form_validation');
	 	
	 	$this->form_validation->set_rules('name','name','required');
	 	if($this->form_validation->run() == FALSE)
	 	{
	 		$this->load->view('manage/index');
	 		$this->load->view('categorial/add');
	 		$this->load->view('manage/footer');
	 	}else{
	 	   $this->db->insert('category',array('name'=>$this->input->post('name'),'ename'=>$this->input->post('ename')));
	 	   redirect('category/index','refresh');
	 	   //echo anchor('category/index','返回');
	 	}
	 }
     
	 public function edit()
	 {
	    
	 	$this->load->helper(array('form','url'));
		$this->load->library('form_validation');
		$this->load->database();
		/*
		$this->form_validation->set_rules('name','name','required');
				
		if($this->form_validation->run() == FALSE)
		{
			$this->db->select('cid,name,ename');
			$this->db->where('cid', $this->uri->segment(4));
			$query = $this->db->get('category');
			$cate = $query->result_array();
			$data['cate'] = $cate[0];
			$this->load->view('manage/index');
			$this->load->view('category/edit',$data);
			$this->load->view('manage/footer');
		}else{
		   $this->db->where('cid',$this->input->post('cid'));
		   $this->db->update('category',array('name'=>$this->input->post('name'),'ename'=>$this->input->post('ename')));
		   echo anchor('category/index','返回');
		}
		*/
	 	    $this->load->helper('url');
           
	 		$cid =$this->input->post("id");
	 		if(!empty($cid)){
	 		$cate = $this->db->select('*')->where('cid',$cid)->get('category')->result_array();
	 		$json = json_encode($cate[0]);
	 		echo $json;
	 		}else{
	 			echo "error";
	 		}
	 	
	 	
	 }
	 
	 public function update()
	 {
	 	$this->db->where('cid',$this->input->post('cid'));
	 	$this->db->update('category',array('name'=>$this->input->post('name'),'ename'=>$this->input->post('ename')));
	 	echo "success";
	 }
	 
	 public function drop()
	 {
	 	$this->load->helper('url');
	 	$this->load->database();
	 	$this->db->where('cid',$this->uri->segment(4))->delete("category");
	 	echo anchor('category/index','返回');
	 }
	 
}